package com.snailct.bpp.router.util;


import com.snailct.bpp.commons.utils.BlockData;
import com.snailct.bpp.router.datachange.CompositeService;
import com.snailct.bpp.router.datachange.RequestVo;
import com.snailct.bpp.router.datachange.ServiceApi;
import com.snailct.bpp.router.datachange.ServiceParam;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.xml.sax.InputSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * xml工具
 * @author andy
 *
 */
public abstract class XMLUtil {
	private XMLUtil(){}
	
	

	
	public static String doc2String(String filePath){
		Format format = Format.getPrettyFormat();
		format.setEncoding("UTF-8");// 设置xml文件的字符为UTF-8，解决中文问题
		XMLOutputter xmlout = new XMLOutputter(format);
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		SAXBuilder builder = new SAXBuilder();
		Document doc;
		try {
			doc = builder.build(filePath);
			xmlout.output(doc, bo);
			return bo.toString();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	public static RequestVo readXml4Input(String xml){
		SAXBuilder builder = new SAXBuilder();
		StringReader sr = new StringReader(xml);
		org.jdom.Document doc;
		RequestVo requestVo = new RequestVo();
		try{
			InputSource is = new InputSource(sr);
			doc = builder.build(is);
			Element rootElement = (Element) doc.getRootElement();
			Element service = rootElement.getChild("serviceId");
			requestVo.setServiceId(service.getText());
			List<?> messageNodes  = rootElement.getChildren("messageNode");
			for(int i=0;i<messageNodes.size();i++){
				Element tempElement = (Element) messageNodes.get(i);

				List<?> properties = tempElement.getChildren();
				ServiceParam param= null;
				for(int j=0;j<properties.size();j++){
					param = new ServiceParam();
					Element serviceElement = (Element)properties.get(j);
					param.setParamName(serviceElement.getName());
					param.setValue(serviceElement.getText());
					requestVo.getParams().add(param);
					System.out.println (serviceElement.getName());
				}

			}

		}catch (Exception e){
			e.printStackTrace();
		}
		return requestVo;
	}
	public static CompositeService readXml4String(String xml,String id){
		SAXBuilder builder = new SAXBuilder();

		org.jdom.Document doc;
		CompositeService compositeService = new CompositeService();
		try{

			doc = builder.build(xml);
			Element rootElement = (Element) doc.getRootElement();
			List<?> composites = rootElement.getChildren("compositeService");
			for(int i=0;i<composites.size();i++){
				Element tempElement = (Element) composites.get(i);
				String beanName = tempElement.getAttributeValue("name");

				compositeService.setBeanName(beanName);
				List<?> services = tempElement.getChildren("service");
				ServiceApi serviceApi= null;
				for(int j=0;j<services.size();j++){
					serviceApi = new ServiceApi();
					Element serviceElement = (Element)services.get(j);
					String methodName = serviceElement.getAttributeValue("methodName");
					serviceApi.setMethodName(methodName);
					compositeService.addServiceApi(serviceApi);
					Element param = serviceElement.getChild("params");
					List<?> properties = param.getChildren("property");
					ServiceParam property = null;
					for(int h=0;h<properties.size();h++){
						Element propElement = (Element)properties.get(h);
						String paramName = propElement.getAttributeValue("name");
						String paramValue = propElement.getText();
						property = new ServiceParam();
						property.setParamName(paramName);
						property.setValue(paramValue);
						serviceApi.addParam(property);
					}
				}

			}

		}catch (Exception e){
			e.printStackTrace();
		}
		return compositeService;
	}

	public static org.jdom.Document stringToXMl(String xmlStr) {
		SAXBuilder builder = new SAXBuilder();
		StringReader sr = new StringReader(xmlStr);
		org.jdom.Document doc=null;
		try{
			InputSource is = new InputSource(sr);
			doc = builder.build(is);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return doc;
	}

	public static List<BlockData> readXml4String(InputStream xml){
		List<BlockData> results = new ArrayList<BlockData>();
		SAXBuilder builder = new SAXBuilder();

		org.jdom.Document doc;

		try{
			InputSource is = new InputSource(xml);
			doc = builder.build(is);
			Element rootElement = (Element) doc.getRootElement();


			List<?> items = rootElement.getChildren("item");
			BlockData blockData = null;
			for(int i=0;i<items.size();i++){
				Element tempElement = (Element) items.get(i);
				List<?> fields = tempElement.getChildren("field");

				for(int j=0;j<fields.size();j++){
					blockData = new BlockData();
					Element field = (Element) fields.get(i);

					results.add(blockData);
				}


			}

		}catch (Exception e){
			e.printStackTrace();
		}
		return results;
	}
}
